﻿using System;
using System.Text;
using System.Collections.Generic;
using TanashCMS.Business;
using TanashCMS.Business.Task;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TanashCMS.Tests.Task
{
    /// <summary>
    /// Summary description for UrlRewriterTaskTest
    /// </summary>
    [TestClass]
    public class UrlRewriterTaskTest
    {
        public UrlRewriterTaskTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void WebPageUrlTest()
        {
            Assert.AreEqual("/website/webpage[@key='about']", new UrlRewriterTask().ConvertToXPath(new Uri("http://localhost:54501/about.html"), CMSObjectType.WebPage));
            Assert.AreEqual("/website/webpage[@key='group1']/webpage[@key='group2']/webpage[@key='page1']", new UrlRewriterTask().ConvertToXPath(new Uri("http://localhost:54501/group1/group2/page1.html"), CMSObjectType.WebPage));
        }

        [TestMethod]
        public void CategoryUrlTest()
        {
            Assert.AreEqual("/website/inventory/category[@key='group1']", new UrlRewriterTask().ConvertToXPath(new Uri("http://localhost:54501/group1.html"), CMSObjectType.Category));
            Assert.AreEqual("/website/inventory/category[@key='test+group']", new UrlRewriterTask().ConvertToXPath(new Uri("http://localhost:54501/test+group.html"), CMSObjectType.Category));
            Assert.AreEqual("/website/inventory/category[@key='group1']/category[@key='group2']", new UrlRewriterTask().ConvertToXPath(new Uri("http://localhost:54501/group1/group2.html"), CMSObjectType.Category));
            Assert.AreEqual("/website/inventory/category[@key='group1']/category[@key='group2']/category[@key='group1']", new UrlRewriterTask().ConvertToXPath(new Uri("http://localhost:54501/group1/group2/group1.html"), CMSObjectType.Category));
            Assert.AreEqual("/website/inventory/category[@key='group1']/category[@key='group2']/category[@key='group3']", new UrlRewriterTask().ConvertToXPath(new Uri("http://localhost:54501/group1/group2/group3.html"), CMSObjectType.Category));
            Assert.AreEqual("/website/inventory/category[@key='group3']/category[@key='group2']/category[@key='group3']", new UrlRewriterTask().ConvertToXPath(new Uri("http://localhost:54501/group3/group2/group3.html"), CMSObjectType.Category));
        }
        [TestMethod]
        public void ProductUrlTest()
        {
            Assert.AreEqual("/website/inventory/category/product[@key='product1']", new UrlRewriterTask().ConvertToXPath(new Uri("http://localhost:54501/product1.html"), CMSObjectType.Product));
            Assert.AreEqual("/website/inventory/category[@key='group1']/product[@key='product1']", new UrlRewriterTask().ConvertToXPath(new Uri("http://localhost:54501/group1/product1.html"), CMSObjectType.Product));
            Assert.AreEqual("/website/inventory/category[@key='group1']/product[@key='group1']", new UrlRewriterTask().ConvertToXPath(new Uri("http://localhost:54501/group1/group1.html"), CMSObjectType.Product));
            Assert.AreEqual("/website/inventory/category[@key='group1']/category[@key='group2']/product[@key='group1']", new UrlRewriterTask().ConvertToXPath(new Uri("http://localhost:54501/group1/group2/group1.html"), CMSObjectType.Product));
            Assert.AreEqual("/website/inventory/category[@key='group1']/category[@key='group2']/category[@key='group3']/product[@key='product1']", new UrlRewriterTask().ConvertToXPath(new Uri("http://localhost:54501/group1/group2/group3/product1.html"), CMSObjectType.Product));
            Assert.AreEqual("/website/inventory/category[@key='test+category']/category[@key='another+test+category']/product[@key='product1+%26+product2']", new UrlRewriterTask().ConvertToXPath(new Uri("http://localhost:54501/test+category/another+test+category/product1+%26+product2.html"), CMSObjectType.Product));
        }
        [TestMethod]
        public void UrlSplitterTest()
        {
            string[] result = UrlRewriterTask.splitUrl("http://something.com/about.html");
            AssertArrayEqual(new string[]{"about.html"}, result, join(result));

            result = UrlRewriterTask.splitUrl("http://something.com/group1/about.html");
            AssertArrayEqual(new string[] { "group1", "about.html" }, result, join(result));

            result = UrlRewriterTask.splitUrl("http://something.com/about/about.html");
            AssertArrayEqual(new string[] { "about", "about.html" }, result, join(result));

            result = UrlRewriterTask.splitUrl("http://something.com/about/group1/about.html");
            AssertArrayEqual(new string[] { "about", "group1", "about.html" }, result, join(result));

            result = UrlRewriterTask.splitUrl("http://something.com/items1/something arbitrary/productpage.html");
            AssertArrayEqual(new string[] { "items1", "something%20arbitrary", "productpage.html" }, result, join(result));
        }

        private string join(string[] a)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string s in a)
                sb.Append(string.Format("{0}{1}", s, ","));
            return sb.ToString();
        }

        public static void AssertArrayEqual(object[] expected, object[] actual, string message)
        {
            Assert.AreEqual(expected.Length, actual.Length);
            for (int i = 0; i < expected.Length; i++)
                Assert.AreEqual(expected[i], actual[i]);
        }
    }
}
